package com.aiatomic.couting.settings;

import com.aiatomic.couting.IConstant;
import com.aiatomic.couting.R;
import com.aiatomic.couting.SettingActivity;

import android.app.Fragment;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentChooseTagColor extends Fragment {
	private View view;

	private SettingActivity activity;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_choose_tag_color, container, false);
		activity = (SettingActivity) this.getActivity();
		TextView txtBack = (TextView) activity.findViewById(R.id.txtBack);
		txtBack.setText(R.string.bottom_setup);
		txtBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				activity.onBackPressed();
			}
		});
		TextView txtNext = (TextView) activity.findViewById(R.id.txtNext);
		txtNext.setVisibility(View.INVISIBLE);

		TextView txtMiddle = (TextView) activity.findViewById(R.id.txtMiddle);
		txtMiddle.setText(R.string.general_settings);
		SharedPreferences shared = activity.getSharedPreferences("setting", activity.MODE_PRIVATE);
		view.findViewById(R.id.view_color_1).setBackgroundColor(shared.getInt(IConstant.COLOR1, Color.RED));
		view.findViewById(R.id.view_color_2).setBackgroundColor(shared.getInt(IConstant.COLOR2, Color.YELLOW));
		view.findViewById(R.id.view_color_3).setBackgroundColor(shared.getInt(IConstant.COLOR3, Color.GREEN));
		view.findViewById(R.id.view_color_4).setBackgroundColor(shared.getInt(IConstant.COLOR4, Color.BLUE));
		bindEvent();
		return view;
	}

	private void bindEvent() {
		view.findViewById(R.id.ll_tag_color_1).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (view.findViewById(R.id.ll_colors1).getVisibility() == View.GONE) {
					view.findViewById(R.id.ll_colors1).setVisibility(View.VISIBLE);
					view.findViewById(R.id.ll_colors2).setVisibility(View.GONE);
					view.findViewById(R.id.ll_colors3).setVisibility(View.GONE);
					view.findViewById(R.id.ll_colors4).setVisibility(View.GONE);
				} else {
					view.findViewById(R.id.ll_colors1).setVisibility(View.GONE);
				}
			}
		});
		view.findViewById(R.id.ll_tag_color_2).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (view.findViewById(R.id.ll_colors2).getVisibility() == View.GONE) {
					view.findViewById(R.id.ll_colors1).setVisibility(View.GONE);
					view.findViewById(R.id.ll_colors2).setVisibility(View.VISIBLE);
					view.findViewById(R.id.ll_colors3).setVisibility(View.GONE);
					view.findViewById(R.id.ll_colors4).setVisibility(View.GONE);
				} else {
					view.findViewById(R.id.ll_colors2).setVisibility(View.GONE);
				}
			}
		});
		view.findViewById(R.id.ll_tag_color_3).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (view.findViewById(R.id.ll_colors3).getVisibility() == View.GONE) {
					view.findViewById(R.id.ll_colors1).setVisibility(View.GONE);
					view.findViewById(R.id.ll_colors2).setVisibility(View.GONE);
					view.findViewById(R.id.ll_colors3).setVisibility(View.VISIBLE);
					view.findViewById(R.id.ll_colors4).setVisibility(View.GONE);
				} else {
					view.findViewById(R.id.ll_colors3).setVisibility(View.GONE);
				}
			}
		});
		view.findViewById(R.id.ll_tag_color_added).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (view.findViewById(R.id.ll_colors4).getVisibility() == View.GONE) {
					view.findViewById(R.id.ll_colors1).setVisibility(View.GONE);
					view.findViewById(R.id.ll_colors2).setVisibility(View.GONE);
					view.findViewById(R.id.ll_colors3).setVisibility(View.GONE);
					view.findViewById(R.id.ll_colors4).setVisibility(View.VISIBLE);
				} else {
					view.findViewById(R.id.ll_colors4).setVisibility(View.GONE);
				}
			}
		});
		view.findViewById(R.id.ll_colors1).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (v instanceof ViewGroup) {
					ViewGroup parent = (ViewGroup) v;
					for (int i = 0; i < parent.getChildCount(); i++) {
						parent.getChildAt(i).setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {
								ColorDrawable color = (ColorDrawable) v.getBackground();
								view.findViewById(R.id.view_color_1).setBackgroundColor(color.getColor());
								SharedPreferences.Editor editor = activity
										.getSharedPreferences("setting", activity.MODE_PRIVATE).edit();
								editor.putInt(IConstant.COLOR1, color.getColor());
								editor.commit();
							}
						});
					}
				}
			}
		});

		view.findViewById(R.id.ll_colors2).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (v instanceof ViewGroup) {
					ViewGroup parent = (ViewGroup) v;
					for (int i = 0; i < parent.getChildCount(); i++) {
						parent.getChildAt(i).setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {
								ColorDrawable color = (ColorDrawable) v.getBackground();
								view.findViewById(R.id.view_color_2).setBackgroundColor(color.getColor());
								SharedPreferences.Editor editor = activity
										.getSharedPreferences("setting", activity.MODE_PRIVATE).edit();
								editor.putInt(IConstant.COLOR2, color.getColor());
								editor.commit();

							}
						});
					}
				}
			}
		});

		view.findViewById(R.id.ll_colors3).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (v instanceof ViewGroup) {
					ViewGroup parent = (ViewGroup) v;
					for (int i = 0; i < parent.getChildCount(); i++) {
						parent.getChildAt(i).setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {
								ColorDrawable color = (ColorDrawable) v.getBackground();
								view.findViewById(R.id.view_color_3).setBackgroundColor(color.getColor());
								SharedPreferences.Editor editor = activity
										.getSharedPreferences("setting", activity.MODE_PRIVATE).edit();
								editor.putInt(IConstant.COLOR3, color.getColor());
								editor.commit();

							}
						});
					}
				}
			}
		});

		view.findViewById(R.id.ll_colors4).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (v instanceof ViewGroup) {
					ViewGroup parent = (ViewGroup) v;
					for (int i = 0; i < parent.getChildCount(); i++) {
						parent.getChildAt(i).setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {
								ColorDrawable color = (ColorDrawable) v.getBackground();
								view.findViewById(R.id.view_color_4).setBackgroundColor(color.getColor());
								SharedPreferences.Editor editor = activity
										.getSharedPreferences("setting", activity.MODE_PRIVATE).edit();
								editor.putInt(IConstant.COLOR4, color.getColor());
								editor.commit();
							}
						});
					}
				}
			}
		});

	}

}
